package web;

import org.hibernate.Hibernate;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.userdetails.UserDetails;

import beans.User;
import dao.UserDAO;

public class LoggedUser {
	public UserDAO userDAO;

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public  User getLoggedUser() {
		Object obj = SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		String username;
		if (obj instanceof UserDetails) {
			username = ((UserDetails) obj).getUsername();
		} else {
			username = obj.toString();
		}
		User user = userDAO.findUserByUsername(username);
	//	Hibernate.initialize(user.getOwnRestaurant());
		return user;
	}

}
